﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Ring2Park.Models;

namespace Ring2Park.Controllers
{ 
    public class ParkingSessionController : Controller
    {
        private Ring2ParkEntities db = new Ring2ParkEntities();

        //
        // GET: /ParkingSessions/

        public ActionResult Index()
        {
            var parkingSessions = from p in db.ParkingSessions
                           where p.UserName == User.Identity.Name
                           select p;

            if (parkingSessions.Count() > 0)
                return View(parkingSessions.ToList());
            else
                return View("NoParkingSessionsFound");
        }

        //
        // GET: /ParkingSessions/Details/5

        public ActionResult Details(int id = 0)
        {
            ParkingSession parkingsession = db.ParkingSessions.Find(id);

            if (parkingsession == null)
                return HttpNotFound();
            else
                return View(parkingsession);
        }

        //
        // GET: /ParkingSessions/Create

        public ActionResult Create()
        {
            PopulateLocationsDropDownList();
            PopulateVehiclesDropDownList();
            return View();
        } 

        //
        // POST: /ParkingSessions/Create

        [HttpPost]
        public ActionResult Create(ParkingSession parkingsession)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    parkingsession.UserName = User.Identity.Name;
                    db.ParkingSessions.Add(parkingsession);
                    db.SaveChanges();
                    return RedirectToAction("Index");
                }
            }
            catch (DataException)
            {
                //Log the error (add a variable name after DataException)        
                ModelState.AddModelError("", "Unable to save changes. Try again, and if the problem persists see your system administrator.");
            }

            PopulateLocationsDropDownList(parkingsession.ParkingLocationId);
            PopulateVehiclesDropDownList(parkingsession.VehicleId);
            return View(parkingsession);
        }
        
        //
        // GET: /ParkingSessions/Edit/5
 
        public ActionResult Edit(int id = 0)
        {
            ParkingSession parkingsession = db.ParkingSessions.Find(id);
            PopulateLocationsDropDownList(parkingsession.ParkingLocationId);
            PopulateVehiclesDropDownList(parkingsession.VehicleId);

            if (parkingsession == null)
                return HttpNotFound();
            else
                return View(parkingsession);
        }

        //
        // POST: /ParkingSessions/Edit/5

        [HttpPost]
        public ActionResult Edit(ParkingSession parkingsession)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    db.Entry(parkingsession).State = EntityState.Modified;
                    db.SaveChanges();
                    return RedirectToAction("Index");
                }
            }
            catch (DataException)
            {
                //Log the error (add a variable name after DataException)        
                ModelState.AddModelError("", "Unable to save changes. Try again, and if the problem persists see your system administrator.");
            }

            PopulateLocationsDropDownList(parkingsession.ParkingLocationId);
            PopulateVehiclesDropDownList(parkingsession.VehicleId);
            return View(parkingsession);
        }

        //
        // GET: /ParkingSessions/Delete/5

        public ActionResult Delete(int id, bool? saveChangesError)
        {
            if (saveChangesError.GetValueOrDefault())
            {
                ViewBag.ErrorMessage = "Unable to save changes. Try again, and if the problem persists see your system administrator.";
            }

            ParkingSession parkingsession = db.ParkingSessions.Find(id);
            if (parkingsession == null)
                return HttpNotFound();
            else
                return View(parkingsession);
        }

        //
        // POST: /ParkingSessions/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {            
            try
            {
                ParkingSession parkingsession = db.ParkingSessions.Find(id);
                db.ParkingSessions.Remove(parkingsession);
                db.SaveChanges();
            }
            catch (DataException)
            {
                //Log the error (add a variable name after DataException)        
                return RedirectToAction("Delete",
                    new System.Web.Routing.RouteValueDictionary {                 
                        { "id", id },                 
                        { "saveChangesError", true } 
                });
            }

            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }

        private void PopulateLocationsDropDownList(object selectedLocation = null)
        {
            var locationsQuery = from l in db.ParkingLocations
                                orderby l.Name
                                select l;
            ViewBag.ParkingLocationId = new SelectList(locationsQuery, "ParkingLocationId", "Name", selectedLocation);
        }

        private void PopulateVehiclesDropDownList(object selectedVehicle = null) 
        { 
            var vehiclesQuery = from v in db.Vehicles
                                where v.UserName == User.Identity.Name
                                orderby v.License 
                                select v; 
            ViewBag.VehicleId = new SelectList(vehiclesQuery, "VehicleId", "License", selectedVehicle); 
        }
    }

}